#include "settingsdialog.h"

// ---------------------- //
// CLASS : SettingsDialog //
// ---------------------- //

//SettingsDialog constructor
SettingsDialog::SettingsDialog(QWidget* parent, QSettings* settings):
    QDialog(parent),
    _settings(settings)
{
    //Widget creation
    _saveFileWidget = new EditSettingsSaveFileWidget(this,_settings->value("saveFileName","save.xml").toString());
    _musicPathWidget = new EditSettingsMusicPathWidget(this,_settings->value("downloadDir",QDir::homePath()).toString());
    _okButton = new QPushButton(tr("OK"),this);
    _cancelButton = new QPushButton(tr("Cancel"),this);

    //Layout management
    QVBoxLayout* layout = new QVBoxLayout(this);
    layout->addWidget(_saveFileWidget);
    layout->addWidget(_musicPathWidget);

    QHBoxLayout* okCancelLayout = new QHBoxLayout();
    okCancelLayout->addWidget(_okButton);
    okCancelLayout->addWidget(_cancelButton);
    layout->addLayout(okCancelLayout);


    //Connections
    connect(_okButton,SIGNAL(clicked()),this,SLOT(saveAndQuit())); //_okButton.clicked()-->saveAndQuit()
    connect(_cancelButton,SIGNAL(clicked()),this,SLOT(close())); //_cancelButton.clicked()-->close()
}

//SLOT - Modify the settings with the values stored in the widgets and exit
void SettingsDialog::saveAndQuit()
{
    _settings->setValue("saveFileName",_saveFileWidget->getSaveFileName());
    _settings->setValue("downloadDir",_musicPathWidget->getMusicPath());

    this->close();
}

// ---------------------------------- //
// CLASS : EditSettingsSaveFileWidget //
// ---------------------------------- //

//EditSettingsSaveFileWidget constructor
EditSettingsSaveFileWidget::EditSettingsSaveFileWidget(QWidget* parent, const QString& saveFileName):
    QWidget(parent)
{
    //Widget creation
    _saveFileNameLabel = new QLabel(tr("Save File"),this);
    _saveFileNameLineEdit = new QLineEdit(saveFileName,this);
	
    //Layout management
    QHBoxLayout* layout = new QHBoxLayout(this);
    layout->addWidget(_saveFileNameLabel);
    layout->addWidget(_saveFileNameLineEdit);
}

//Getter to the content of the QLineEdit
QString EditSettingsSaveFileWidget::getSaveFileName()
{
    return _saveFileNameLineEdit->text();
}

// ----------------------------------- //
// CLASS : EditSettingsMusicPathWidget //
// ----------------------------------- //

//EditSettingsMusicPathWidget constructor
EditSettingsMusicPathWidget::EditSettingsMusicPathWidget(QWidget* parent, const QString& currentPath):
    QWidget(parent)
{
    //Widget creation
    _musicPathLabel = new QLabel(tr("Music Path"),this);
    _musicPathLineEdit = new QLineEdit(currentPath,this);
    _musicPathLineEdit->setReadOnly(true);
    _editMusicPathButton = new QPushButton(tr("Choose Path"),this);
	
    //Layout management
    QHBoxLayout* layout = new QHBoxLayout(this);
    layout->addWidget(_musicPathLabel);
    layout->addWidget(_musicPathLineEdit);
    layout->addWidget(_editMusicPathButton);
	
    //Connections
    connect(_editMusicPathButton,SIGNAL(clicked()),this,SLOT(changePath())); //_editMusicPathButton.clicked()-->changePath()
}

//Getter to the selected path
QString EditSettingsMusicPathWidget::getMusicPath()
{
    return _musicPathLineEdit->text();
}

//SLOT - Open a dialog to select a path
void EditSettingsMusicPathWidget::changePath()
{
    _musicPathLineEdit->setText(QFileDialog::getExistingDirectory(this->parentWidget(),
                                                                  tr("Select save directory"),
                                                                  _musicPathLineEdit->text()));
}
